Deployment Status Apache License Documentation Status Python Online Python version—Jan 27, 2021


Copyright © Wei MEI, MLMS™—all rights reserved. 🀤

Handling conditions

Conditional execution can be completed using the if statement

if syntax

>>> if expression:
# code to execute
>>> else:
# code to execute
```

Comparison operators

  • < less than
  • < greater than
  • == is equal to
  • >= greater than or equal to
  • <= less than or equal to
  • != not equal to
1
2
3
4
5
6
7
country = input('Enter the name of your home country: ')
if country == 'canada':
	# string comparisons are case sensitive
	# if you typed in CANADA or Canada it will not match
	print('So you must like hockey!')
else:
	print('You are not from Canada')
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#Calculate the tax
# Anything purchased for more than $1.00 is charged a 7% tax
price = input('how much did you pay? ')

# Convert the string to a number
price = float(price)

# Check if the price is greater than 1.00
if price >= 1.00:
	# Everything over $1.00 is charged 7% tax
	tax = .07
	print('Tax rate is: ' + str(tax))

Demo: dates

1
2
3
4
5
6
7
8
country = 'CANADA'
# by converting the string entered to lowercase and comparing it to a string
# that is all lowercase letters I make the comparison case-insensitive
# If someone types in CANADA or Canada it will still be a match
if country.lower() == 'canada':
	print('Hello eh')
else:
	print('Hello')
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
price = input('how much did you pay? ')
price = float(price)

if price >= 1.00:
	# Anything that costs $1.00 or more is charged 7% tax
	# All statements indented are only executed if price is > = 1
	tax = .07
	print('Tax rate is: ' + str(tax))
else:
	# Anything else we do not charge any tax
	# All statements indented are only executed if price is NOT >= 1 
	tax = 0
	print('Tax rate is: ' + str(tax))


1
2
3
4
5
6
7
8
price = 5.0
if price >= 1.00:
	tax = .07
else:
	tax = 0
# the print statement below is not indented so is executed after the if 
# statement is evaluated
print(tax)

PPT Demonstrations

Challenges time

Check the following script and try to find the mistake:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Fix the mistakes in this code and test based on the description below
# If I enter 2.00 I should see the message "Tax rate is: 0.07" 
# If I enter 1.00 I should see the message "Tax rate is: 0.07" 
# If I enter 0.50 I should see the message "Tax rate is: 0" 
price = input('how much did you pay? ')

if price > 1.00:
	tax = .07
	print('Tax rate is: ' + str(tax))
else
	tax = 0
print('Tax rate is: ' + str(tax))

solutions:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Fix the mistakes in this code and test using the following
# If I enter 2.00 I should see the message "Tax rate is: 0.07" 
# If I enter 1.00 I should see the message "Tax rate is: 0.07" 
# If I enter 0.50 I should see the message "Tax rate is: 0" 
price = input('how much did you pay? ')
price = float(price)

if price >= 1.00:
	tax = .07
	print('Tax rate is: ' + str(tax))
else:
    tax = 0
    print('Tax rate is: ' + str(tax))